package com.idis.dbApp.controller;

import com.idis.dbApp.mapper.UserMapper;
import com.idis.entity.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by Ming on 2018/1/28.
 */
@RestController
@RequestMapping("/user")
@Api(tags = "登录用户接口")
public class UserController {

    @Autowired
    UserMapper userMapper;

    @ApiOperation("获取所有用户信息")
    @GetMapping("/all")
    public List<User> finAllUser(){
        return userMapper.findAll();
    }

    @ApiOperation("根据用户名搜搜用户")
    @GetMapping("/username/{username}")
    public List<User> searchByUsername(@ApiParam("用户名")@PathVariable("username")String username){
        return userMapper.findByUsername(username);
    }

    @ApiOperation("新增用户")
    @PostMapping("/save")
    public int saveUser(@ApiParam("用户信息")@RequestBody User user){
        return userMapper.insert(user);
    }

    @ApiOperation("更新用户的非空信息")
    @PostMapping("/updatenotnull")
    public Integer updateNotNull(@ApiParam("用户信息，若某信息不需要更新则可置空，主要用于更改其他信息时不对密码进行修改，,ID必须")@RequestBody User user){
        return userMapper.updateByPrimaryKeySelective(user);
    }

    @ApiOperation("更新用户的信息")
    @PostMapping("/update")
    public Integer update(@ApiParam("用户信息，ID必须")@RequestBody User user){
        return userMapper.updateByPrimaryKey(user);
    }

    @ApiOperation("根据属性查询用户")
    @PostMapping("/findbyproperty")
    public List<User> findByProperty(@ApiParam("需要进行查询的用户属性，若某信息无需用于查询，则可置空")@RequestBody User user){
        return userMapper.findByProperty(user);
    }

    @ApiOperation("删除用户")
    @PostMapping("/delete")
    public Integer delete(@ApiParam("用户信息，需要且仅需要用户ID")@RequestBody User user){
        return userMapper.deleteByPrimaryKey(user.getId());
    }
}
